package hb;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import zhuzhuwang.HibernateSessionFactory;

public class AskRentListDao {
	private SessionFactory sessions=null;
	private Session mySession=null;
	private Transaction tx;
	
	public AskRentListDao(){
		sessions=new Configuration().configure().buildSessionFactory();

	}
	
	public void insert(AskRentList need){
		
		mySession=sessions.openSession();	
		tx=mySession.beginTransaction();
		try{
			mySession.save(need);
			tx.commit();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			mySession.close();
		}
	}
	
	public AskRentList getHouse(String lid){
		
		AskRentList need=new AskRentList();
		
		mySession=sessions.openSession();		
		String hql="from AskRentList u where u.listId =:lid";
		Query query=mySession.createQuery(hql);
		query.setParameter("lid", Integer.parseInt(lid));
		
		List list=query.list();
		Iterator it=list.iterator();
		if(it.hasNext())
		{
			need=(AskRentList) it.next();
		}
		mySession.close();
		
		return need;
	}
	
	
	public List getAll(){
		List list=null;
		
		mySession=sessions.openSession();		
		String hql="from AskRentList u";
		Query query=mySession.createQuery(hql);
		
		list=query.list();
		mySession.close();
		
		return list;
	}
	
	public List getSearch(String search){
		List list=null;
		
		mySession=sessions.openSession();		
		String hql="from AskRentList u where u.description like :search ";
		Query query=mySession.createQuery(hql);
		
		query.setParameter("search", "%"+search+"%");
		
		list=query.list();
		mySession.close();
		
		return list;
	}
		
	public void update(AskRentList need){
		mySession=sessions.openSession();
		tx=mySession.beginTransaction();
		mySession.update(need);
		tx.commit();
		mySession.close();
	}
	
	public void delete(AskRentList need){
		mySession=sessions.openSession();
		tx=mySession.beginTransaction();
		mySession.delete(need);
		tx.commit();
		mySession.close();
	}
	
	public List getNeed(int district,int rent,int room){
		List list=null;
		String hql=null;
		String strDistrict = null;
		int strRent1=0;
		int strRent2=0;
		int strRoom=0;
		int condition1=0;

		int condition3=0;
		
		switch(district){
		case 1:strDistrict="1";break;
		case 2:strDistrict="2";break;
		case 3:strDistrict="3";break;
		case 4:strDistrict="4";break;
		case 5:strDistrict="5";break;
		case 6:strDistrict="6";break;
		case 7:strDistrict="7";break;
		case 8:strDistrict="8";break;
		case 9:strDistrict="9";break;
		case 10:strDistrict="10";break;
		case 11:strDistrict="11";break;
		case 12:strDistrict="12";break;
		case 13:strDistrict="13";break;
		case 14:strDistrict="14";break;
		case 15:strDistrict="15";break;
		case 16:strDistrict="16";break;
		case 17:strDistrict="17";break;
		case 18:strDistrict="18";break;
		case 19:strDistrict="19";break;
        default:strDistrict="0";condition1=1;break;
		}
		
		switch(rent){
		case 1:strRent1=0;strRent2=500;break;
		case 2:strRent1=500;strRent2=1000;break;
		case 3:strRent1=1000;strRent2=2000;break;
		case 4:strRent1=2000;strRent2=3000;break;
		case 5:strRent1=3000;strRent2=4000;break;
		case 6:strRent1=4000;strRent2=6000;break;
		case 7:strRent1=6000;strRent2=99999;break;
		default:strRent1=0;strRent2=99999;break;
		}
		
		switch(room){
		case 1:strRoom=1;break;
		case 2:strRoom=2;break;
		case 3:strRoom=3;break;
		case 4:strRoom=4;condition3=1;break;
		default:strRoom=0;condition3=1;break;
		}
		
		mySession=sessions.openSession();		
		hql="from AskRentList u where u.rent>=:rent1 and u.rent<:rent2 and u.status=0";
		if(condition1==0)
			hql=hql+" and u.region=:district";
		if(condition3==0)
			hql=hql+" and u.bedroom=:room";
		else if(condition3==1)
			hql=hql+" and u.bedroom>=:room";

		System.out.println(hql);
		
		Query query=mySession.createQuery(hql);
		
		if(condition1==0)
			query.setParameter("district", strDistrict);
		
		query.setParameter("rent1", strRent1);
		query.setParameter("rent2", strRent2);
		query.setParameter("room", strRoom);
		
		list=query.list();
		mySession.close();
		
		return list;
	}
	
	public List getMyNeed(String owner){
		List list=null;
		
		UserDao userDao=new UserDao();
		
		mySession=sessions.openSession();		
		String hql="from AskRentList u where u.user=:uid";
		
		Query query=mySession.createQuery(hql);
		
		User userID=userDao.getUser(owner);
		
		query.setParameter("uid", userID);
		
		list=query.list();
		
		mySession.close();
		
		return list;
	}

	
}
